﻿Imports System.Transactions
Public Class ForceCopyTask
    Public Sub ForceCopy(ByVal _jobListRow As Job.JobListRow)
        Dim sqlall As String = ""
        Dim sqlbd As String = ""
        Dim sqlrev As String = ""
        Dim count As Integer = 0
        Dim batchadap As New FundCenter2TableAdapters.FCReleasesTableAdapter
        Dim adap2 As New checkPlanDSTableAdapters.FundCenterReleasesTableAdapter
        Dim adapter As New FundCenter2TableAdapters.BudgetDetailsTableAdapter
        Dim dtrow As New FundCenter2.BudgetDetailsDataTable
        Dim newVersion As Integer = _jobListRow("ToVersion").ToString()
        Dim adapRel As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter
        Dim rfadap As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter
        Dim copyBatch As New CopyBatch()
        Dim dd As New DataTable
        Dim Myversion As Integer
        Dim username As String = _jobListRow("SubmitBy").ToString()
        Dim planYear As String = _jobListRow("PlanYear").ToString()
        Dim top_fct As String
        Dim version As Integer
        copyBatch.GetTopFct(username, planYear, 1, top_fct, version)

        Dim roleType As String = ""
        Dim adapUsers As New UserTableAdapters.UsersTableAdapter
        Dim dtUsers As New User.UsersDataTable
        dtUsers = adapUsers.GetDataUser(username)
        If dtUsers.Rows.Count() > 0 Then
            roleType = dtUsers(0)("RoleType")
        End If
        If Val(roleType) = 5 Or Val(roleType) = 1 Then
            top_fct = "0000"
        End If

        Dim FCTDataSet As New DataSet
        copyBatch.GetChildWithVersion(top_fct, planYear, 1, FCTDataSet, Myversion)

        dd = FCTDataSet.Tables("Items")

        Dim topcount As Integer = 0
        topcount = Val(adapRel.CheckFundRelease(dd.Rows(0).Item("FundCenterCode"), Val(planYear), newVersion))
        If topcount > 0 Then
            Throw New Exception("ระบบไม่สามารถ Copy Version ที่ทำการ Copy Version มาแล้วได้ หากต้องการ Copy Version อีกครั้งให้ทำการ Release Version ก่อน จากนั้นทำการ Copy Version อีกครั้ง")
            Exit Sub
        End If

        Using scope As New TransactionScope()
            Try
                For Each row As DataRow In dd.Rows
                    Dim chBudg As Integer = 0
                    chBudg = rfadap.CheckFundRelease(row.Item("FundCenterCode"), planYear, newVersion)
                    If chBudg = 0 Then
                        Dim ReleaseF As String
                        Try
                            ReleaseF = row.Item("ReleaseFlag")
                        Catch ex As Exception
                            ReleaseF = "N"
                        End Try

                        Dim causeForce As String = _jobListRow("ForceReason").ToString()
                        If ReleaseF <> "Y" Then
                            sqlall += copyBatch.BatchForceFlag("Y", Date.Now, "BATCH", "Y", causeForce, row.Item("FundCenterCode"), planYear, row.Item("Now_version"))
                        End If

                        dtrow = adapter.GetBudget_id(row.Item("FundCenterCode"), planYear, row.Item("Now_version"))
                        If dtrow.Rows.Count = 0 Then
                            'dtrow = adapter.GetBudget_id2(row.Item("FundCenterCode"), planYear) ' Comment By Orn 29042558 เพราะ copy ข้อมูลมาไม่ครบ
                            dtrow = adapter.GetBudget_id3(row.Item("FundCenterCode"), planYear)
                        End If
                        For Each rowId As FundCenter2.BudgetDetailsRow In dtrow
                            Dim newId As Integer = adapter.copyVersion(newVersion, rowId.Id)
                            sqlbd += copyBatch.BatchcopyAtt(newId, rowId.Id)
                            sqlbd += copyBatch.BatchcopyB12(newVersion, newId, rowId.Id)
                        Next

                        Try
                            batchadap.Execute(sqlbd)
                        Catch ex As Exception
                        End Try

                        sqlbd = ""
                        Dim adapRev As New FundCenter2TableAdapters.BudgetRevPlansTableAdapter
                        Dim dtRev As New FundCenter2.BudgetRevPlansDataTable
                        dtRev = adapRev.GetDataBy_Func(row.Item("FundCenterCode"), planYear, row.Item("Now_version"))
                        If dtRev.Rows.Count = 0 Then
                            dtRev = adapRev.GetDataBy_Func2(row.Item("FundCenterCode"), planYear)
                        End If
                        For Each rowIdR As FundCenter2.BudgetRevPlansRow In dtRev
                            sqlrev += copyBatch.BatchCopyRev(newVersion, rowIdR.Id)
                        Next
                        Try
                            batchadap.Execute(sqlrev)
                        Catch ex As Exception
                        End Try

                        sqlrev = ""

                        ''Comment By Orn 19/05/2558 
                        'Dim preVer As String = row.Item("Now_version")
                        'sqlall += copyBatch.BatchForce(planYear, "N", "Y", newVersion, preVer, Date.Now, "BATCH", row.Item("FundCenterCode"), Val(planYear), "N", causeForce)
                        'Dim exist As Integer
                        'exist = adap2.checkExist(row.Item("FundCenterCode"), Val(planYear), row.Item("Now_version"))
                        'If exist = 0 Then
                        '    sqlall += copyBatch.BatchForce(planYear, "Y", "N", row.Item("Now_version"), "0", Date.Now, "BATCH", row.Item("FundCenterCode"), planYear, "Y", causeForce)
                        'Else
                        '    sqlall += copyBatch.BatchOldRelease("N", Date.Now, "BATCH", row.Item("FundCenterCode"), planYear, row.Item("Now_version"))
                        'End If

                        'FundcenterRelease
                        'Dim maxPreVersion As Integer
                        'maxPreVersion = adap2.CheckMaxVersionNoForce(row.Item("FundCenterCode"), Val(planYear))
                        For index As Integer = newVersion To 1 Step -1
                            Dim exist As Integer
                            exist = adap2.checkExist(row.Item("FundCenterCode"), Val(planYear), index)
                            If exist = 0 Then
                                sqlall += copyBatch.BatchForce(planYear, If(newVersion = index, "N", "Y"), If(newVersion = index, "Y", "N"), index, If(newVersion = index, index - 1, "0"), Date.Now, "BATCH", row.Item("FundCenterCode"), planYear, If(newVersion = index, "N", "Y"), causeForce)
                            Else
                                sqlall += copyBatch.BatchOldRelease("N", Date.Now, "BATCH", row.Item("FundCenterCode"), planYear, index)
                            End If
                        Next
                        
                    End If
                Next
                Try
                    batchadap.Execute(sqlall)
                Catch ex As Exception
                End Try
                scope.Complete()
            Catch ex As Exception
                Transaction.Current.Rollback()
                Throw New Exception(ex.Message)
            End Try
        End Using
    End Sub
    
End Class

